#!/usr/bin/python
#-*- coding:utf-8 -*-

# activity_window.py 
#
# Copyright (c) 2007 Marcelo Lira dos Santos
#
# Author: Marcelo Lira dos Santos <setanta@gmail.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA

import pygtk
pygtk.require('2.0')
import gtk
import gtk.glade
import gtkhtml2

import os
from activity import *

class ActivityWindow:

    activity = None

    def __init__(self, gladefile, activity=None):
        self.wtree = gtk.glade.XML(gladefile, 'activityWindow')

        #Widgets -------
        self.window = self.wtree.get_widget('activityWindow')

        self.activity_label = self.wtree.get_widget('activity_label')

        # Information tab
        self.author_label = self.wtree.get_widget('author_label')
        self.age_label = self.wtree.get_widget('age_label')
        self.duration_label = self.wtree.get_widget('duration_label')
        self.contents_label = self.wtree.get_widget('contents_label')
        self.software_label = self.wtree.get_widget('software_label')

        # Goals tab
        self.goals_textview = self.wtree.get_widget('goals_textview')

        # Procedure tab
        self.procedure_scroll = self.wtree.get_widget('procedure_scroll')
        self.procedure_view = gtkhtml2.View()
        self.procedure_scroll.add(self.procedure_view)
        self.procedure_doc = gtkhtml2.Document()
        self.procedure_view.set_document(self.procedure_doc)

        # Evaluation tab
        self.evaluation_textview = self.wtree.get_widget('evaluation_textview')

        #Autoconnect Signals and Callbacks
        self.wtree.signal_autoconnect(self)

        self.set_activity(activity)

        self.window.show_all()


    # Callbacks ---------------------------------------------------------------
    def on_startActivity_clicked(self, widget):
        self.start_activity()


    # Auxiliary Methods -------------------------------------------------------
    def _create_procedure_widget(self):
        procedure_widget = moz.MozEmbed()
        procedure_widget.load_url("http://www.google.com/")

        return procedure_widget


    # Methods -----------------------------------------------------------------
    def set_icon(self, icon):
        self.window.set_icon(icon)

    def set_activity(self, activity):
        self.activity = activity

        self.activity_label.set_markup('<big><b>%s</b></big>' % activity.title)
        self.author_label.set_text(activity.author)
        self.age_label.set_text(activity.age)
        self.duration_label.set_text(activity.duration)
        self.contents_label.set_text(activity.description)
        self.software_label.set_text(activity.program)

        #self.goals_textbuffer.set_text(activity.goals)
        #self.evaluation_textbuffer.set_text(activity.evaluation)

        self.procedure_doc.clear()
        self.procedure_doc.open_stream('text/html')
        stream = '<html><head></head><body>' + activity.procedures + '</body></html>'
        self.procedure_doc.write_stream(stream)
        self.procedure_doc.close_stream()


    def start_activity(self):
        print 'start_activity: ', self.activity.program
        #print os.popen4(self.activity.program)


def main():
    aw = ActivityWindow()
    gtk.main()


if __name__ == '__main__':
    main()

